
#include "../../engine.h"

#include "../../User.h"

#include "../../NLogic/QuestInfo.h"

#include "world.h"

namespace ProjectZ { namespace NCommand { namespace NWorld {

void CS_REQ_QUEST_DATA::OnExecute(User * pUser, ::Network::Packet * pPacket)
{

	/*
	 * CS_REQ_QUEST_DATA	0x2000			/// 퀘스트 데이터 받음
		{

		}

		SC_REQ_QUEST_DATA	0x2001			/// 퀘스트 데이터 받음
		{

			u8 cont;
			State [MAX : TOTAL_QUEST_COUNT = 500] /// 500까지 할지 결정 해야됨
			{
				u16 		u16_tid;	// QUEST_ID
				//s8    		s8_state; // 필요하지 않아 제거함.
			}

			u8 cont;
			Quest [MAX : 12] : 내가 가지고 있는 퀘스트
			{
			u8		    u8_idx;			/// index
				u16                 u16_tid;			/// QUEST_ID
				u8                  u8_achieve;         	/// 달성카운트
			}
		}

	 */

	STRESSLOG("[CHANNEL] CS_REQ_QUEST_DATA::ONEXECUTE");

	PREPARE_PACKET;

	NLogic::QuestInfo * pQuestInfo = pUser->GetQuestInfo();
	if (NULL == pQuestInfo)
	{
		return;
	}

	uint8_t count = pQuestInfo->GetQuestHistoryCount();
	if (0 == count)
	{
		// DB Query
		if (false == pQuestInfo->QuestHistoryLoadFromDatabase())
			return;
	}
	count = pQuestInfo->GetQuestHistoryCount();

	DEBUGLOG("[CHANNEL] CS_REQ_QUEST_DATA::ONEXECUTE GetQuestHistoryCount : %d", count);

	::Network::Packet * pQuestInfoAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	* pQuestInfoAck << count;

	auto var = pQuestInfo->GetQuestHistoryVector().begin();
	for (; var != pQuestInfo->GetQuestHistoryVector().end(); ++ var)
	{
		// packet build and send
		* pQuestInfoAck << static_cast<uint16_t>( * var );
	}

	count = pQuestInfo->GetQuestCount();
	if (0 == count)
	{
		// DB Query
		if (false == pQuestInfo->QuestLoadFromDatabase())
			return;
	}
	count = pQuestInfo->GetQuestCount();

	* pQuestInfoAck << count;

	auto var2 = pQuestInfo->GetQuestTree().begin();
	for (; var2 != pQuestInfo->GetQuestTree().end(); ++ var2)
	{
		// packet build and send
		NLogic::QuestInfo::Quest::Data & data = var2->second->GetData();
		* pQuestInfoAck << static_cast<uint8_t>(data._idx) << static_cast<uint16_t>(data._questID) << static_cast<uint8_t>(data._achieve);
		DEBUGLOG("NLogic::QuestInfo::Data : %d", data._questID);
	}
	pQuestInfoAck->MakePacket(CMD_SC_REQ_QUEST_DATA);
	pUser->Send(pQuestInfoAck);
}

} /*NWorld*/ } /*NCommand*/ } /*ProjectZ*/
